﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface;

namespace MaxStudio.Concrete.Server.Service
{
    public class ReceivableService : ServiceHandle, IReceivable
    {
        #region IReceivable Members

    //    ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

        public OperationResult<List<tb_Receivable>> GetAllReceivable()
        {
            OperationResult<List<tb_Receivable>> result = new OperationResult<List<tb_Receivable>>();
            ConcreteDaoDataContext dataContext = getDateContext();
            List<tb_Receivable> list = new List<tb_Receivable>();
            try
            {
                list = dataContext.GetAllReceivable();
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_Receivable> GetReceivableById(int id)
        {
            OperationResult<tb_Receivable> result = new OperationResult<tb_Receivable>();
            ConcreteDaoDataContext dataContext = getDateContext();
            tb_Receivable receivable;
            try
            {
                receivable = dataContext.GetReceivableById(id);
                if (receivable == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = receivable;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_Receivable> CreateReceivable(tb_Receivable entity)
        {
            OperationResult<tb_Receivable> result = new OperationResult<tb_Receivable>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.CreateReceivable(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Receivable> ModifyReceivable(tb_Receivable entity)
        {
            OperationResult<tb_Receivable> result = new OperationResult<tb_Receivable>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.ModifyReceivable(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Receivable> DeleteReceivable(tb_Receivable entity)
        {
            OperationResult<tb_Receivable> result = new OperationResult<tb_Receivable>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.DeleteReceivable(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        #endregion
    }
}
